package com.navinfo.opentsp.platform.computing.serializer;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Charsets;
import com.navinfo.opentsp.platform.computing.util.DateUtils;
import com.navinfo.opentsp.platform.location.protocol.common.LCObdInformation;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.serialization.EventSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStream;

/**
 * obdInformation信息
 * HDFS_Sink的obdInformation序列化器，拼接成字段间通过\001分割的字符串
 */
public class HDFSObdInformationSerializer implements EventSerializer {
    private static final Logger logger = LoggerFactory.getLogger(HDFSObdInformationSerializer.class);

    private final String APPEND_NEWLINE = "appendNewline";
    private final boolean appendNewline;
    private final String splitStr = "\001";
    private final OutputStream out;

    private HDFSObdInformationSerializer(OutputStream out, Context ctx) {
        this.appendNewline = ctx.getBoolean(APPEND_NEWLINE, true);
        this.out = out;
    }

    @Override
    public boolean supportsReopen() {
        return false;
    }

    @Override
    public void afterCreate() {
    }

    @Override
    public void afterReopen() {
    }

    @Override
    public void beforeClose() {
    }

    @Override
    public void flush() throws IOException {
        this.out.flush();
    }

    @Override
    public void write(Event event) throws IOException {
        try {
            //拼写落盘到HDFS的字符串。字段间分隔符是\001，null通过\N表示
            LCObdInformation.ObdInformation obdInformation = LCObdInformation.ObdInformation.parseFrom(event.getBody());
            StringBuilder obdInformationStr = new StringBuilder(5000);
            obdInformationStr.append(obdInformation.getProtocolType()).append(splitStr);
            obdInformationStr.append(obdInformation.getMilType()).append(splitStr);
            obdInformationStr.append(obdInformation.getDiagnosticSupportStatus()).append(splitStr);
            obdInformationStr.append(obdInformation.getDiagnosticReadinessStatus()).append(splitStr);
            obdInformationStr.append(obdInformation.getCalid()).append(splitStr);
            obdInformationStr.append(obdInformation.getVin()).append(splitStr);
            obdInformationStr.append(obdInformation.getCalibrationIdentifyNO()).append(splitStr);
            obdInformationStr.append(obdInformation.getCvn()).append(splitStr);
            obdInformationStr.append(JSON.toJSON(obdInformation.getIuprList())).append(splitStr);
            obdInformationStr.append(obdInformation.getFaultCount()).append(splitStr);
            obdInformationStr.append(JSON.toJSON(obdInformation.getFaultCodesList())).append(splitStr);
            obdInformationStr.append(obdInformation.getIsPatch()).append(splitStr);
            obdInformationStr.append(obdInformation.getGpsTime()).append(splitStr);
            obdInformationStr.append(obdInformation.getEncrypt()).append(splitStr);
            obdInformationStr.append(obdInformation.getTerminalVersion()).append(splitStr);
            obdInformationStr.append(obdInformation.getTerminalId()).append(splitStr);
            obdInformationStr.append(obdInformation.getSerialNumber()).append(splitStr);
            obdInformationStr.append(obdInformation.getRLength()).append(splitStr);
            obdInformationStr.append(obdInformation.getSLength()).append(splitStr);
            obdInformationStr.append(obdInformation.getRValue()).append(splitStr);
            obdInformationStr.append(obdInformation.getSValue()).append(splitStr);

            //添加字段只能在分割线之上添加,注意时间分区这块，终端传的时候是秒还是毫秒
            //------------------------------------------分割线----------------------------------------
            obdInformationStr.append(DateUtils.format(obdInformation.getGpsTime() * 1000, "yyyyMMdd"));

            //将数据拼接后写入hdfs
            this.out.write(obdInformationStr.toString().getBytes(Charsets.UTF_8));
            if (this.appendNewline) {
                this.out.write(10);
            }
        } catch (Exception e) {
            logger.error("执行HDFS的HDFSObdInformationSerializer拦截器失败：" + e.getMessage(), e);
        }
    }

    public static class Builder implements EventSerializer.Builder {
        public Builder() {
        }

        @Override
        public EventSerializer build(Context context, OutputStream out) {
            return new HDFSObdInformationSerializer(out, context);
        }
    }
}
